package org.example;


import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.json.JSONObject;
import io.socket.client.Socket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Hello world!
 */
public class App {

    private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

    public static AtomicInteger count = new AtomicInteger();

    public static void main(String[] args) {
        int s = args.length == 0?1:Integer.parseInt(args[0]);
        int e = args.length == 0?2:Integer.parseInt(args[1]);

        for(int i=s;i<=e;i++){
            new Thread(
                    new Runnable() {
                @Override
                public void run() {
                    int num = count.incrementAndGet();
                    Client client = new Client(num);

                    // 建立连接
                    Socket socket = client.init();

                    // 1主动监听类
                    // 自定义事件`event1` -> 接收服务端广播消息
                    socket.on("chatevent", objects -> {
//                        LOGGER.info("收到chatevent：" + Arrays.toString(objects));
                    }
                    );
                    int k = 1;
                    while (true) {
                        try {
                            TimeUnit.MILLISECONDS.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        JSONObject json = new JSONObject();
                        json.putOpt("userId", "user_"+num);
                        json.putOpt("message", Thread.currentThread().getName()+" num="+num+"_xxxx_" + k);
                        json.putOpt("dest", "user_"+num);
                        socket.emit("chatevent", json);
//                        LOGGER.info(" send {}", json);
                        k++;
                    }
                }
            }).start();
        }


        try {
            TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException interruptedException) {
            interruptedException.printStackTrace();
        }

    }

}

